﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;

using DotSpatial.Topology;
using DotSpatial.Data;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

 


namespace FXnaGis
{
    public class MoveEventArgs : EventArgs
    {

        public MoveEventArgs(double xOff,double yOff)
        {
            this._xOffset = xOff;
            this._yOffset = yOff;
 
        }





        double _xOffset;

        public double XOffset
        {
            get { return _xOffset; }
            set { _xOffset = value; }
        }


        double _yOffset;

        public double YOffset
        {
            get { return _yOffset; }
            set { _yOffset = value; }
        }


    }



    /// <summary>
    /// 为移动操作提供绘制操作支持
    /// 这里我们设计的先2一点，呵呵，先出效果
    /// 
    /// </summary>
    public class FMoveMF:FSelectMF
    {






        public FMoveMF(FMapFrame frame)
            : base(frame)
        {


 

        }



        #region events

        public event EventHandler<MoveEventArgs> SelectedShapeMoved;

        #endregion


        protected override void OnActivate()
        {
            base.OnActivate();

            //激活时，需要获取选择区域

            this._selectedExtent = this._geoMapFrame.LayerService.SelectedExtent;

         


            this._geoMapFrame.Parent.Invalidate();



        }

        protected override void OnDeactivate()
        {
            base.OnDeactivate();


            this._selectedExtent = null;

            this._geoMapFrame.Parent.Invalidate();

        }


        bool _moveDragMode = false;


        //需要取得所有已经选择了的物体的空间范围

        Extent _selectedExtent;



        //移动的偏移量,虽然在外部定义，但其实并没有使用


     


        Coordinate _downCoord;


        System.Drawing.Point _mousePosition;
  

  





        protected override void OnSelectionChanged()
        {
            base.OnSelectionChanged();

            this._selectedExtent = this._geoMapFrame.LayerService.SelectedExtent;

            if (this._selectedExtent!=null)
            {

                Rectangle rect = this._geoMapFrame.ProjToBuffer(this._selectedExtent);

                rect = rect.ExpandBy(20);

                this._geoMapFrame.Parent.Invalidate(rect);



            }

       



        }
        


        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {

          



            if (e.Button==System.Windows.Forms.MouseButtons.Left)
            {

                if (this.IsOnTop(e.Location))
                {

                    this._moveDragMode = true;

                    if (this._moveDragMode)
                    {

                       

                        this._downCoord = this._geoMapFrame.BufferToProj(e.Location);
                        
                    }

                    
                }
              

                else
                {

                    //如果在外围，则使用默认选择机制

                    base.OnMouseDown(e);
 

                }


               
                
            }
        }
        


        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {


                //弹出离开时，不需要判断


                if (this._moveDragMode)
                {

                    this.Move(e.Location);


                    this._moveDragMode = false;

                    this._geoMapFrame.Parent.Invalidate();

                    
                }


                base.OnMouseUp(e);

                //if (this.IsOnTop(e.Location))
                //{

                    

                //}
                //else
                //{
                //    base.OnMouseUp(e);
                   
                //}

            }

        }


        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {

            //初期的鼠标状态判断

            if (this.IsOnTop(e.Location))
            {
                this._geoMapFrame.Parent.Cursor = Cursors.Cross;




            }
            else
            {
                this._geoMapFrame.Parent.Cursor = Cursors.Default;

            }




            //如果开始移动
            if (this._moveDragMode)
            {


                this._mousePosition = e.Location;

               


                this._geoMapFrame.Parent.Cursor = Cursors.Cross;

                this._geoMapFrame.Parent.Invalidate();


            }


          



            base.OnMouseMove(e);
           
        }


        protected override void OnDraw(FMapDrawArgs e)
        {


            if (this._selectedExtent != null)
            {
                



             



                //查找中心点

                Coordinate c = this._selectedExtent.Center;

                System.Drawing.Point ptCenter = this._geoMapFrame.ProjToBuffer(c);

                //X轴
                //e.Graphics.DrawLine(xPen, ptCenter.X, ptCenter.Y, ptCenter.X + 100, ptCenter.Y);

                GDIHelper.DrawXAxisArrow(e.Graphics,Pens.Red,ptCenter,new System.Drawing. Point (ptCenter.X + 100, ptCenter.Y));
                

                //Y轴


                //e.Graphics.DrawLine(Pens.Green, ptCenter.X,ptCenter.Y, ptCenter.X, ptCenter.Y - 100);


                GDIHelper.DrawYAxisArrow(e.Graphics, Pens.Green, ptCenter, new System.Drawing.Point(ptCenter.X, ptCenter.Y - 100));


                //X轴上的线

                e.Graphics.DrawLine(Pens.Red, ptCenter.X + 30, ptCenter.Y, ptCenter.X + 30, ptCenter.Y - 30);


                //Y轴上的线

                e.Graphics.DrawLine(Pens.Green, ptCenter.X, ptCenter.Y-30, ptCenter.X+30, ptCenter.Y - 30);




              

               

            }


            //绘制辅助移动图形

            if (this._moveDragMode)
            {


                //当前正在移动,则按照偏移情况，绘制虚拟图形

                int xOffset;

                int yOffset;


                System.Drawing.Point ptOld = this._geoMapFrame.ProjToBuffer(_downCoord);

                xOffset = this._mousePosition.X - ptOld.X;
                yOffset = this._mousePosition.Y - ptOld.Y;


                Matrix org = e.Graphics.Transform;

                Matrix shift = org.Clone();

                shift.Translate(xOffset,yOffset);


                e.Graphics.Transform = shift;


                this.DrawSelectedRect(e.Graphics);


                e.Graphics.Transform = org;








            }
            else
            {


              
 

            }


            //绘制选择的逻辑

            base.OnDraw(e);





        }


        #region 私有函数


        bool IsOnTop(System.Drawing.Point pt)
        {

            if (this._selectedExtent==null)
            {
                return false;
                
            }


            Coordinate c = this._selectedExtent.Center;

            System.Drawing.Point ptCenter = this._geoMapFrame.ProjToBuffer(c);

            
            //矩形特殊说明，这里取的是工作矩形的四分之一，右上角部分，来绘制移动坐标轴

            Rectangle rect = new Rectangle(ptCenter.X,ptCenter.Y-100,100,100);

            return rect.Contains(pt);
          
        }



        void Move(System.Drawing.Point ptEnd)
        {

            int xOffset;

            int yOffset;


            System.Drawing.Point ptOld = this._geoMapFrame.ProjToBuffer(_downCoord);

            xOffset = ptEnd.X - ptOld.X;
            yOffset = ptEnd.Y - ptOld.Y;

            if (xOffset==0&&yOffset==0)
            {
                return;
                
            }

            //转换为地理坐标,这里需要注意一点是，屏幕坐标的变换，跟地理坐标，的正，负数关系


            double xPre = this._geoMapFrame.BufferToProj(xOffset);
            double yPre = this._geoMapFrame.BufferToProj(yOffset)*(-1);


            if (this.SelectedShapeMoved!=null)
            {
                SelectedShapeMoved(this,new MoveEventArgs(xPre,yPre));
                
            }


            

 
        }

        



        #endregion


    }
}
